package com.lfs.utils;

import com.lfs.domain.DailyDataManagement;
import com.lfs.domain.MonthQuestionnaire;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ConvertUtil {

    private static final Logger log = LoggerFactory.getLogger(ConvertUtil.class);

    /**
     * 读取的每日数据 转化为 月度数据
     * @param data 每日数据
     * @return 月度数据
     */
    public static List<List<MonthQuestionnaire>> modifyData(List<DailyDataManagement> data) {
        Map<String, List<DailyDataManagement>> userMap = data.stream().collect(Collectors.groupingBy(DailyDataManagement::getPerson));
        ArrayList<List<MonthQuestionnaire>> result = new ArrayList<>();
        userMap.forEach((name, collect) -> {
            List<MonthQuestionnaire> list = new ArrayList<>();
            for (DailyDataManagement dailyDataManagement : collect) {
                int np = Integer.parseInt(dailyDataManagement.getNp()==null?"0":dailyDataManagement.getNp());
                int op = Integer.parseInt(dailyDataManagement.getOp()==null?"0":dailyDataManagement.getOp());
                if (np!=0) {
                    add(dailyDataManagement,np,list,"新患者");
                }
                if (op!=0) {
                    add(dailyDataManagement,op,list,"老患者");
                }
            }
            result.add(list);
        });
        return result;
    }
    private static void add(DailyDataManagement dailyDataManagement,Integer count,List<MonthQuestionnaire> list,String adType) {
        for (int i = 0; i < count; i++) {
            list.add(MonthQuestionnaire.builder()
                    .dr("李吉昌")
                    .mr(dailyDataManagement.getPerson())
                    .date(dateTrans(dailyDataManagement.getDate()))
                    .hospital(MapUtils.hospital(dailyDataManagement.getHospital()))
                    .name(dailyDataManagement.getHcp())
                    .symptom(dailyDataManagement.getIndication())
                    .adType(adType)
                    .insurance("否")
                    .build());
        }
    }




    /**
     * excel写入对象转换
     * @param monthQuestionnaires 写入对象集合
     * @return 嵌套集合
     * @throws IllegalAccessException
     */
    public static List<List<String>> convertToExcelData(List<MonthQuestionnaire> monthQuestionnaires) throws IllegalAccessException {
        List<List<String>> data = new ArrayList<>();
        Field[] fields = MonthQuestionnaire.class.getDeclaredFields();

        for (MonthQuestionnaire mq : monthQuestionnaires) {
            List<String> row = new ArrayList<>();
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(mq);
                row.add(value != null ? value.toString() : "");
            }
            data.add(row);
        }
        return data;
    }


    /**
     * 将2025.1.1类型的时间 转化为 2025年1月1日类型
     * @param time 字符串时间
     * @return 年月日字符串时间
     */
    public static String timeTrans(String time){
        String[] split = time.split("\\.");
        return split[0]+"年"+split[1]+"月"+split[2]+"日";
    }

    /**
     * 将2025.1.1类型的时间 转化为 2025年1月1日类型
     * @param time 字符串时间
     * @return 日期
     */
    public static Date dateTrans(String time){
        //指定日期格式为"yyyy.MM.dd"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        try {
            return sdf.parse(time);
        } catch (ParseException e) {
            // 捕获解析异常并处理
            log.warn("日期解析异常", e);
            return null;
        }
    }
}
